<template>
  <Dialog
    v-model="show"
    @confirm="handleSubmit"
    @close="close"
    title="审核退款申请"
    width="600px"
  >
    <div class="h-500 overflow-y-auto">
      <el-form :model="form" label-width="120px">
        <el-form-item label="是否同意申请">
          <el-radio-group v-model="form.refundStatus">
            <el-radio :label="-3">同意</el-radio>
            <el-radio :label="-4">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="驳回原因" v-if="form.refundStatus === -4">
          <el-input
            v-model="form.refundCheckRejectReason"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item label="商家收货地址" v-if="form.refundStatus === -3">
          <div class="address flex1">
            <div
              class="address-item flex_aic cursor-pointer"
              :class="{ active: form.refundCheckAddressId === item.id }"
              v-for="item in addressList"
              :key="item.id"
              @click="form.refundCheckAddressId = item.id"
            >
              <el-icon
                size="16"
                :color="
                  form.refundCheckAddressId === item.id ? '#ff7e28' : '#999'
                "
                class="mr10"
                ><CircleCheckFilled
              /></el-icon>
              <div class="flex1 ml10">
                <div class="flex_aic">
                  <span>{{ item["name" + locale] }}</span>
                  <span class="ml10">{{ item["phone"] }}</span>
                </div>
                <div class="address-item-address">
                  {{ item["province" + locale] }} {{ item["city" + locale] }}
                  {{ item["area" + locale] }} {{ item["address" + locale] }}
                </div>
              </div>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </Dialog>
</template>
<script setup>
import { ref, watch, onMounted } from "vue";
import Dialog from "@/components/dialog.vue";
import Api from "@/config/api/api";
import { useI18n } from "vue-i18n";
import { CircleCheckFilled } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

const { locale } = useI18n();
const show = defineModel();
const props = defineProps({
  detail: {
    type: Object,
    default: () => ({}),
  },
});
const addressList = ref([]);

const form = ref({
  refundStatus: -3,
  refundCheckRejectReason: "",
  refundCheckAddressId: "",
});
watch(show, (val) => {
  if (val) {
    getAddressList();
  }
});
const handleSubmit = () => {
  const params = {
    ...form.value,
    id: props.detail.id,
    refundCheckPayTotal: props.detail.refundReal || 0,
  };
  if (form.value.refundStatus === -4) {
    params.refundCheckAddressId = "";
  }
  Api.refundCheck(params).then((res) => {
    if (res.status === 200) {
      ElMessage.success("退款审核成功");
      show.value = false;
    }
  });
};
const close = () => {
  form.value = {
    refundStatus: -3,
    refundCheckRejectReason: "",
    refundCheckAddressId: "",
  };
  show.value = false;
};

const getAddressList = () => {
  Api.getTenantAddress({
    page: 0,
    size: 1000,
  }).then((res) => {
    form.value.refundCheckAddressId = res.data?.content?.[0]?.id;
    addressList.value = res.data?.content || [];
  });
};
</script>
<style lang="scss" scoped>
.h-500 {
  height: 500px;
}
.lht-38 {
  line-height: 38px;
}
.w-120 {
  width: 120px;
}
.address {
  .address-item {
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    margin-bottom: 10px;
    .address-item-address {
      font-size: 12px;
      color: #999;
    }

    &:hover {
      border-color: #ff7e28;
    }
    &.active {
      border-color: #ff7e28;
    }
  }
}
</style>
